﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;

namespace CMS.Web.CompDenu
{
    public partial class AddXLDonThu : BaseUserControl, IAddXLDonThuView
    {
        private AddXLDonThuPresenter presenter;
        IList<CachXL> xlList;
        IList<PhongBan> pbList;
        IList<NguoiSuDung> nsdList;
        NguoiSuDungInfo userInfo = null;
        String donthuID = "";
        String xlID = "";
        ChuyenTiep _chuyenTiep = null;
        DonThu _donThu = null;

        public AddXLDonThu()
        {
            this.presenter = new AddXLDonThuPresenter(this, NHibernateDaoFactory.Instance.GetChuyenTiepDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            donthuID = Request.QueryString.Get("objID") == null ? "" : Request.QueryString.Get("objID");
            xlID = Request.QueryString.Get("xlID") == null ? "" : Request.QueryString.Get("xlID");
            
            this.RaiseClientEvent();
            _donThu = DonThuDao.Instance.GetById(donthuID);
            if (!xlID.Equals(""))
            {
                this.lblMess.InnerHtml = "";
                _chuyenTiep = ChuyenTiepDao.Instance.GetById(xlID);
                //if (_donThu.ChuyenTieps.Count > 0)
                //{
                //    _chuyenTiep = _donThu.ChuyenTieps[0];
                //}
                //else
                //{
                //    _chuyenTiep = new ChuyenTiep();
                //    _chuyenTiep.MaDonThu = _donThu.ID;
                //}
                // Get nguoi gui don
                //_nguoiGuiDon = _donThu.NguoiGuiLookup;
            }
            if (!this.IsPostBack)
            {
                if (xlID.Equals(""))
                {
                    this.InitForm(); 
                }
                else
                {
                    InitFormEdit(_chuyenTiep);
                }                
            }
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            rowMess.Visible = false;
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddXLDonThuPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        public int LoaiPhieu
        {
            get { return 0;// drpLoaiPhieu.SelectedIndex; 
            }
        }
        /// <summary>
        /// Save button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            Response.Redirect(BaseUrl + "&tab=2&type=grid&action=EDIT&objID=" + ID);            
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            //Response.Redirect(BaseUrl + "&action=EDIT&objID=" + ID);
            Response.Redirect(BaseUrl + "&tab=2&type=pro&action=EDIT&objID=" + ID);         
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&tab=2&type=grid&action=EDIT&objID=" + ID);
        }

        protected void InitForm()
        {
            txtDeXuatYKien.Text = "";
            txtYKienLDVP.Text = "";
            txtYKienLDUBND.Text = "";
            txtKetQuaGiaiQuyet.Text = "";
            txtNoiChuyen.Text = "";
            txtLyDoTra.Text = "";
            txtVBGNV.Text = "";
            chkNgayHoanThanh.Checked = false;

            // For CachXL
            this.drpCachXL.DataSource = this.CachXLList;
            drpCachXL.DataTextField = "TenCachXL";
            drpCachXL.DataValueField = "ID";
            this.drpCachXL.DataBind();
            this.drpCachXL.Items.Insert(0, new ListItem("", "0"));

            // For Linh vuc
            this.drpPhongBan.DataSource = this.PhongBanList;
            drpPhongBan.DataTextField = "TenPhongBan";
            drpPhongBan.DataValueField = "ID";
            this.drpPhongBan.DataBind();
            this.drpPhongBan.Items.Insert(0, new ListItem("", "0"));
        }                
        
        protected void InitFormEdit(ChuyenTiep obj)
        {

            txtNgayThuLy.Text = (obj.NgayThuLy != null) ? ((DateTime)obj.NgayThuLy).ToString("dd/MM/yyyy") : "";
            txtNgayHoanTra.Text = (obj.NgayHoanTra != null) ? ((DateTime)obj.NgayHoanTra).ToString("dd/MM/yyyy") : "";
            txtNgayChuyenKN.Text = (obj.NgayChuyenKN != null) ? ((DateTime)obj.NgayChuyenKN).ToString("dd/MM/yyyy") : "";
            txtNgayThongBao.Text = (obj.NgayThongBao != null) ? ((DateTime)obj.NgayThongBao).ToString("dd/MM/yyyy") : "";

            txtNgayNhanKet.Text = (obj.NgayNhanKQ != null) ? ((DateTime)obj.NgayNhanKQ).ToString("dd/MM/yyyy") : "";
            txtNgayHoanThanh.Text = (obj.NgayHoanThanh != null) ? ((DateTime)obj.NgayHoanThanh).ToString("dd/MM/yyyy") : "";
            txtTaiLieuTra.Text = obj.TaiLieuTra;
            txtVBGNV.Text = obj.VBGNV;
            chkNgayHoanThanh.Checked = obj.KhongGioiHan;

            // For CachXL
            ListItem item = null;
            this.drpCachXL.DataSource = this.CachXLList;
            drpCachXL.DataTextField = "TenCachXL";
            drpCachXL.DataValueField = "ID";
            this.drpCachXL.DataBind();
            this.drpCachXL.Items.Insert(0, new ListItem("", "0"));
            item = drpCachXL.Items.FindByValue(obj.MaCachXL.ToString());
            if (item != null) item.Selected = true;

            // For Phong ban
            this.drpPhongBan.DataSource = this.PhongBanList;
            drpPhongBan.DataTextField = "TenPhongBan";
            drpPhongBan.DataValueField = "ID";
            this.drpPhongBan.DataBind();
            this.drpPhongBan.Items.Insert(0, new ListItem("", "0"));
            item = drpPhongBan.Items.FindByValue(obj.MaPhongBan);
            if (item != null) item.Selected = true;

            txtDeXuatYKien.Text = obj.DeXuatYKien;
            txtNoiChuyen.Text = obj.NoiChuyen;
            txtLyDoTra.Text = obj.LyDoTra;
            txtYKienLDVP.Text = obj.YKienLDVP;
            txtYKienLDUBND.Text = obj.YKienLDUBND;
            //set loai hinh            
            txtKetQuaGiaiQuyet.Text = obj.KetQuaGiaiQuyet;
        }

        #region IAddXLDonThuView Members

        public DonThu DonThuToAdd
        {
            get
            {
                try
                {
                    if (txtNgayHoanThanh.Text.Trim().Equals(""))
                    {
                        _donThu.NgayHoanThanh = null;
                    }
                    else
                    {
                        string[] temp = txtNgayHoanThanh.Text.Trim().Split(new Char[] { '/' });
                        _donThu.NgayHoanThanh = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayNhanKet.Text.Trim().Equals(""))
                    {
                        _donThu.NgayNhanKQ = null;
                    }
                    else
                    {
                        string[] temp = txtNgayNhanKet.Text.Trim().Split(new Char[] { '/' });
                        _donThu.NgayNhanKQ = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }

                if (drpPhongBan.SelectedValue.Equals("0"))
                    _donThu.DonViGiaiQuyet = "";
                else
                {
                    ListItem item = drpPhongBan.Items.FindByValue(drpPhongBan.SelectedValue);
                    if (item != null)
                        _donThu.DonViGiaiQuyet = item.Text;
                    else
                        _donThu.DonViGiaiQuyet = "";
                }

                _donThu.VBGNV = txtVBGNV.Text.Trim();
                _donThu.KetQuaGiaiQuyet = txtKetQuaGiaiQuyet.Text.Trim();
                return _donThu;
            }
            set
            {
                this._donThu = value;
            }
        }
        
        public IList<CachXL> CachXLList
        {
            get { return this.xlList; }
            set { this.xlList = value; }
        }
        // For Phong ban
        public IList<PhongBan> PhongBanList
        {
            get { return this.pbList; }
            set { this.pbList = value; }
        }

        // For Nguoi su dung
        public IList<NguoiSuDung> NguoiSuDungList
        {
            get { return this.nsdList; }
            set { this.nsdList = value; }
        }
        // For Nguoi su dung
        public string getTenNguoiSuDung(string code)
        {
            return presenter.getNguoiSuDung(code).TenNguoiSuDung;
        }

        public String ID
        {
            get { return this.donthuID; }
            set { this.ID = value; }
        }

        public String ProgID
        {
            get { return this.xlID; }
            set { this.ProgID = value; }
        }

        public ChuyenTiep ChuyenTiepToEdit
        {
            get
            {
                return _chuyenTiep;
            }
            set
            {
                this._chuyenTiep = value;
            }
        }

        public ChuyenTiep ChuyenTiepToAdd
        {
            get
            {
                ChuyenTiep dtObj;

                if (ChuyenTiepToEdit != null)
                {
                    dtObj = ChuyenTiepToEdit;
                }
                else
                {
                    dtObj = new ChuyenTiep();
                    dtObj.NgayChuyen = DateTime.Now;
                }
                string[] temp = null;
                try
                {
                    if (txtNgayThuLy.Text.Trim().Equals(""))
                    {
                        dtObj.NgayThuLy = null;
                    }
                    else
                    {
                        temp = txtNgayThuLy.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayThuLy = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayHoanTra.Text.Trim().Equals(""))
                    {
                        dtObj.NgayHoanTra = null;
                    }
                    else
                    {
                        temp = txtNgayHoanTra.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayHoanTra = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayChuyenKN.Text.Trim().Equals(""))
                    {
                        dtObj.NgayChuyenKN = null;
                    }
                    else
                    {
                        temp = txtNgayChuyenKN.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayChuyenKN = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayThongBao.Text.Trim().Equals(""))
                    {
                        dtObj.NgayThongBao = null;
                    }
                    else
                    {
                        temp = txtNgayThongBao.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayThongBao = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayNhanKet.Text.Trim().Equals(""))
                    {
                        dtObj.NgayNhanKQ = null;
                    }
                    else
                    {
                        temp = txtNgayNhanKet.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayNhanKQ = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayHoanThanh.Text.Trim().Equals(""))
                    {
                        dtObj.NgayHoanThanh = null;
                    }
                    else
                    {
                        temp = txtNgayHoanThanh.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayHoanThanh = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }

                dtObj.VBGNV = txtVBGNV.Text;
                dtObj.KhongGioiHan = chkNgayHoanThanh.Checked;
                dtObj.YKienLDVP = txtYKienLDVP.Text.Trim();
                dtObj.YKienLDUBND = txtYKienLDUBND.Text.Trim();
                dtObj.DeXuatYKien = txtDeXuatYKien.Text.Trim();
                dtObj.NoiChuyen = txtNoiChuyen.Text.Trim();
                dtObj.TaiLieuTra = txtTaiLieuTra.Text.Trim();
                dtObj.LyDoTra = txtLyDoTra.Text.Trim();
                if (drpPhongBan.SelectedValue.Equals("0"))
                    dtObj.MaPhongBan = null;
                else
                    dtObj.MaPhongBan = drpPhongBan.SelectedValue;
                
                if (drpCachXL.SelectedValue.Equals("0"))
                    dtObj.MaCachXL = null;
                else
                    dtObj.MaCachXL = Convert.ToInt32(drpCachXL.SelectedValue);                
                dtObj.KetQuaGiaiQuyet = txtKetQuaGiaiQuyet.Text.Trim();
                
                return dtObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        #endregion
    }
}